package com.zzyl.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;


import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        List<NursingProjectVo> list = nursingProjectMapper.selectByPage(pageNum, pageSize, name, status);
        for (NursingProjectVo nursingProject : list) {
            ;
            nursingProject.setCount(nursingProjectMapper.selectForPlan(nursingProject.getId()));
        }
        Page<NursingProjectVo> pages = (Page<NursingProjectVo>) list;
        return PageResponse.of(pages, NursingProjectVo.class);
    }

    @Override
    public NursingProjectVo selectNursingProjectById(Long id) {

        return nursingProjectMapper.selectNursingProjectById(id);
    }

    @Override
    public void deleteNursingProjectById(Long id) {
        nursingProjectMapper.deleteNursingProjectById(id);

    }

    @Override
    public void addNursingProject(NursingProjectDto nursingProject) {
//        NursingProject nursingProject = new NursingProject();
//        BeanUtils.copyProperties(nursingProjectDTO, nursingProject);
        NursingProject bean = BeanUtil.toBean(nursingProject, NursingProject.class);
        int count = nursingProjectMapper.selectProject(nursingProject.getName());
        if (count>0){
            throw new RuntimeException("已存在相同名称的项目");
        }
        nursingProjectMapper.addNursingProject(bean);
    }

    @Override
    public void updateNursingProject(NursingProjectDto nursingProject) {
        NursingProject bean = BeanUtil.toBean(nursingProject, NursingProject.class);
        try {
            int count = nursingProjectMapper.selectProject(nursingProject.getName());
            if (count>1){
                throw new RuntimeException("已存在相同名称的项目");
            }
            nursingProjectMapper.updateNursingProject(bean);
        } catch (RuntimeException e) {
            throw new RuntimeException("数据异常");
        }
    }

    @Override
    public void setStatus(Long id, Integer status) {
        nursingProjectMapper.setStatus(id,status);
    }

    @Override
    public List<NursingProjectVo> selectNursingProjectAll() {
        return nursingProjectMapper.selectNursingProjectAll();
    }
}
